<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------

namespace app\api\model;

use app\api\controller\Common;
use think\Db;
use \think\Model;


class FreeGroup extends Model
{
    public function getLineList($keyword='',$page=1,$offset=10)
    {
        $where['is_use'] = 1;
        $where['is_deleted'] = 0;
        $where['is_free'] = 1;//自由拼线路

        if(!empty($keyword)){
            $where['g.title'] = ['like','%'.$keyword.'%'];
        }
        $total = db("group_tours_line g")->where($where)->count();
        $list = db("group_tours_line g")
            ->where($where)
            ->join("city c",'c.id=g.city_id','left')
            ->field("g.line_id,g.title,g.img,c.id as city_id,c.name as city_name,c.kbx_id,g.days,g.trip_id,g.description")
            ->order("g.sort_num desc,g.line_id desc")
            ->page($page,$offset)
            ->select();
        return [$total,$list];
    }

    public function getList($keyword='',$page=1,$offset=10)
    {
        $where['fg.is_deleted'] = 0;
        $where['fg.status'] = ['lt',10];
        if(!empty($keyword)){
            $where['fg.free_title'] = ['like','%'.$keyword.'%'];
        }
        //只显示当前时间提前一天之后的所有时间
        $where['start_date'] = ['egt',date('Y-m-d',strtotime('-1 day'))];
        $total = db("free_group fg")->where($where)->count();
        $list = db("free_group fg")
            ->where($where)
            ->join('member m','m.member_id=fg.owner_id','left')
            ->join('city s','s.id=fg.start_city','left')
            ->join('city e','e.id=fg.end_city','left')
            ->join('group_tours_line g','g.line_id=fg.grouptour_id','left')
            ->field('fg.id,fg.grouptour_id,free_title,free_desc,remark,start_date,
            fg.meet_address,fg.max_people_num,fg.cur_people_num,
            fg.start_city,fg.end_city,fg.status,g.img,
            s.name as start_city_name,e.name as end_city_name,m.member_nickname')
            ->order("fg.is_recommend desc,fg.create_time desc")
            ->page($page,$offset)
            ->select();
        //隐藏用户的昵称
        foreach ($list as $key=>$value){
            $list[$key]['member_nickname'] = (new Common())->hideLongStringMiddlePart($value['member_nickname']);
            //增加其他城市
            $list[$key]['start_city_name'] = $value['start_city_name']?:'';
            $list[$key]['end_city_name'] = $value['end_city_name']?:'';
        }
        return [$total,$list];
    }

    public function getFreeGroupTypeName($type=0)
    {
        if(empty($type)) return '';
        $data = ['1'=>'车找人','2'=>'人找车'];
        $name = $data[$type]??'';
        return $name;
    }

    //推荐同一日期不同类型的拼游
    public function showRecFreeGroupList($freeGroupId,$limit=3)
    {
        if(empty($freeGroupId)) return [];
        $freeGroup = db('free_group')->find($freeGroupId);
        if(empty($freeGroup)) return [];

        if(!in_array($freeGroup['type'],[1,2])) return [];
        if(empty($freeGroup['start_city']) || empty($freeGroup['end_city']) || empty($freeGroup['start_date'])) return [];

        $where['fg.is_deleted'] = 0;
        $where['fg.status'] = 1;
        $where['fg.start_city'] = $freeGroup['start_city'];
        $where['fg.end_city'] = $freeGroup['end_city'];
        $where['fg.start_date'] = $freeGroup['start_date'];
        $where['fg.type'] = $freeGroup['type']==2?3:2;

        $list = db("free_group fg")
            ->where($where)
            ->join('member m','m.member_id=fg.owner_id','left')
            ->join('city s','s.id=fg.start_city','left')
            ->join('city e','e.id=fg.end_city','left')
            ->join('group_tours_line g','g.line_id=fg.grouptour_id','left')
            ->field('fg.id,fg.grouptour_id,free_title,free_desc,remark,start_date,
            fg.meet_address,fg.max_people_num,fg.cur_people_num,
            fg.start_city,fg.end_city,fg.status,g.img,fg.type as tourType,
            s.name as start_city_name,e.name as end_city_name,m.member_nickname')
            ->order("fg.is_recommend desc,fg.create_time desc")
            ->limit($limit)
            ->select();
        //隐藏用户的昵称
        foreach ($list as $key=>$value){
            $list[$key]['member_nickname'] = (new Common())->hideLongStringMiddlePart($value['member_nickname']);
            //增加其他城市
            $list[$key]['start_city_name'] = $value['start_city_name']?:'其他';
            $list[$key]['end_city_name'] = $value['end_city_name']?:'其他';
            //拼游类型
            $list[$key]['tourTypeName'] = $this->getFreeGroupTypeName($value['tourType']);
        }

        return $list;
    }
}